package com.warren.beans.factory.support;

import com.warren.beans.factory.FactoryBean;
import com.warren.beans.factory.StringValueResolver;
import com.warren.error.BeansException;
import com.warren.beans.factory.config.BeanDefinition;
import com.warren.beans.factory.config.BeanPostProcessor;
import com.warren.beans.factory.config.ConfigurableBeanFactory;
import com.warren.util.ClassUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author warren
 * @create 2021-08-17 20:57
 * @Description 抽象类定义模板方法(AbstractBeanFactory)
 */
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    /** ClassLoader to resolve bean class names with, if necessary */
    private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();

    /** BeanPostProcessors to apply in createBean */
    private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();

    /**
     * 应用的字符串解析器，例如注释属性值
     */
    private final List<StringValueResolver> embeddedValueResolvers = new ArrayList<>();

    //主要是对单例  Bean  对象的获取以及在获取不到时需要拿到  Bean
    //的定义做相应  Bean  实例化操作。那么  getBean  并没有自身的去实现这些方法，
    //而是只定义了调用过程以及提供了抽象方法，由实现此抽象类的其他类做相应实
    //现。
    @Override
    public Object getBean(String name) throws BeansException {
       return doGetBean(name,null);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return doGetBean(name,args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return (T) getBean(name);
    }

    protected <T> T doGetBean(final String name, final Object[] args){
        Object sharedInstance = getSingleton(name);
        if (sharedInstance != null) {
            // 如果是 FactoryBean，则需要调用 FactoryBean#getObject
            return (T) getObjectForBeanInstance(sharedInstance, name);
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        Object bean = createBean(name,beanDefinition,args);
        return (T) getObjectForBeanInstance(bean, name);
    }

    /**
     * 获取 FactoryBean 的操作
     * @param beanInstance
     * @param beanName
     * @return
     */
    private Object getObjectForBeanInstance(Object beanInstance, String beanName) {
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }

        Object object = getCachedObjectForFactoryBean(beanName);

        if (object == null) {
            FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
            object = getObjectFromFactoryBean(factoryBean, beanName);
        }

        return object;
    }

    @Override
    public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor){
        //先删除原有的，再添加新的
        this.beanPostProcessors.remove(beanPostProcessor);
        this.beanPostProcessors.add(beanPostProcessor);
    }

    @Override
    public void addEmbeddedValueResolver(StringValueResolver valueResolver) {
        this.embeddedValueResolvers.add(valueResolver);
    }

    @Override
    public String resolveEmbeddedValue(String value) {
        String result = value;
        for (StringValueResolver resolver : this.embeddedValueResolvers) {
            result = resolver.resolveStringValue(result);
        }
        return result;
    }

    /**
     * Return the list of BeanPostProcessors that will get applied
     * to beans created with this factory.
     */
    public List<BeanPostProcessor> getBeanPostProcessors() {
        return this.beanPostProcessors;
    }

    protected abstract  BeanDefinition getBeanDefinition(String beanName) throws  BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;

    public ClassLoader getBeanClassLoader() {
        return beanClassLoader;
    }


}
